#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <pthread.h>

int main(void){
	printf("long long size: %zd\n", sizeof(long long));			//long long size: 8
	printf("long long* size: %zd\n", sizeof(long long*));		//long long* size: 8
	printf("int size: %zd\n", sizeof(int));						//int size: 4
	printf("int* size: %zd\n", sizeof(int*));					//int* size: 8
	printf("unsigned long long size: %zd\n", sizeof( unsigned long long));			//unsigned long long size: 8
	printf("unsigned long long* size: %zd\n", sizeof(unsigned long long*));			//unsigned long long* size: 8


	unsigned long long *pPrimes = NULL;							//Pointer to primes storage area
	unsigned long long trial = 0; 								//Integer to be tested
	bool found = false;											//Indicates when we find a prime
	int total = 0;												//Number of primes required
	int count = 0;												//NUmber of primes found

	printf("How many primes would you like - you'll get at least 4?");
	scanf("%d",&total);											//Total is how many we need to find
	total = total<4?4:total;									//Make sure it is at least 4


	//Allocate sufficient memory to store the number of primes required
	pPrimes = (unsigned long long*)malloc(total*sizeof(unsigned long long));


	if(!pPrimes){ //分配内存不成功
		printf("Not enough memory. It's the end I'm afraid.\n");
		return 1;
	}

	// We know the first three primes so let's give the program a start
	*pPrimes = 2ULL;											//First prime
	*(pPrimes+1) = 3ULL;										//Second prime
	*(pPrimes+2) = 5ULL;										//Third prime
	count = 3;													//NUmber of primes stored
	trial = 5;													//Set to the last prime we have

	//Find all the primes required
	while(count < total){
		trial += 2;												//Next value for checking

		//Divide by the primes we have. If any divide exactly - it's not prime
		for(int i=1;i<count;i++){
			if(!(found = (trial % *(pPrimes+i))))
				break;											//Exit if zero remainder
		}

		if(found)												//we got one - if found is true
			*(pPrimes + count++) = trial;						//Store it and increment count

	}

	//Display primes 5-up
	for(int i=0;i<total;i++){
		printf("%12u",*(pPrimes + i) );
		if(!((i+1) % 5))
			printf("\n");										//Newline after every 5
	}	
	printf("\n");												//Release the heap memory....
	free(pPrimes);												//... and reset the pointer
	pPrimes = NULL;
	return 0;



	//刚分配是不会占用系统内存
	// pPrimes = (unsigned long long*)malloc(10*1024*1024*sizeof(unsigned long long)); //80M
	// printf("分配完内存\n");
	// sleep(10);   //单位秒



	// printf("释放内存\n");
	// free(pPrimes);
	// sleep(10);

	return 0;







}